﻿using System;

namespace test3
{
    abstract class Shape
    {
        public abstract double CalculateArea();
    }
    class Rectangle : Shape
    {
        public double length;
        public double width;
        public Rectangle(double length, double width)
        {
            this.length = length;
            this.width = width;
        }
        public override double CalculateArea()
        {
            double area = length * width;
            return area;
        }
    }
    class Square : Shape
    {
        public double length;
        public Square(double length)
        {
            this.length = length;
        }

        public override double CalculateArea()
        {
            double area = length * length;
            return area;
        }
    }
    class Triangle : Shape
    {
        public double a, b, c;
        public Triangle(double a, double b, double c)
        {
            this.a = a;
            this.b = b;
            this.c = c;
        }
        public override double CalculateArea()
        {
            double s = (a + b + c) / 2;
            double area = Math.Sqrt(s * (s - a) * (s - b) * (s - c));
            return area;
        }
    }

    class Factory
    {
        public static Shape GetProduct(int arg)
        {
            Shape product = null;
            Random r = new Random();
            if (arg == 0)
            {
                double length = 10 * r.NextDouble();
                double width = 10 * r.NextDouble();
                product = new Rectangle(length, width);
            }
            else if (arg == 1)
            {
                double len = 10 * r.NextDouble();
                product = new Square(len);
            }
            else if (arg == 2)
            {
                double a, b, c;
                while (true)
                {
                    a = 10 * r.NextDouble();
                    b = 10 * r.NextDouble();
                    c = 10 * r.NextDouble();
                    if (a + b > c && a + c > b && b + c > a)
                    {
                        break;
                    }
                }
                product = new Triangle(a, b, c);
            }
            return product;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Shape product;
            Random r = new Random();
            double sum = 0;
            for (int i = 0; i < 10; i++)
            {
                int a = r.Next(0, 3);
                product = Factory.GetProduct(a);
                if (a == 0)
                {
                    Console.WriteLine("长方形面积：" + product.CalculateArea());
                }
                else if (a == 1)
                {
                    Console.WriteLine("正方形面积：" + product.CalculateArea());
                }
                else if (a == 2)
                {
                    Console.WriteLine("三角形面积：" + product.CalculateArea());
                }
                sum += product.CalculateArea();
            }
            Console.WriteLine("总面积为：" + sum);
        }
    }
}
